package com.lagou.edu.course.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.common.util.ConvertUtils;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.*;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.entity.po.Teacher;
import com.lagou.edu.course.entity.po.Course;
import com.lagou.edu.course.remote.CourseService;
import com.lagou.edu.course.service.ICourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.lagou.edu.course.service.ITeacherService;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController implements CourseRemoteService {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ICourseService iCourseService;

    @Autowired
    private ITeacherService teacherService;

    /**
     * 获取选课列表
     * @param userId
     * @return
     */
    @GetMapping("/getAllCourses")
    public List<CourseDTO> getAllCourses(@RequestParam(required = false,name = "userId") Integer userId) {
        return  courseService.getAllCourses(userId);
    }

  
    /**
     * 获取已购课程信息
     * @param userId
     * @return
     */
    @GetMapping("/getPurchasedCourse")
    public List<CourseDTO> getPurchasedCourse(@RequestParam("userId") Integer userId){
        return  courseService.getPurchasedCourse(userId);
    }

    /**
     * 新增和修改
     * @param courseDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/saveOrUpdateCourse",method = RequestMethod.POST)
    public boolean saveOrUpdateCourse(@RequestBody CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtil.copyProperties(courseDTO,course);
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        boolean res = this.iCourseService.saveOrUpdate(course);
        TeacherDTO teacherDTO = courseDTO.getTeacherDTO();
        if(teacherDTO == null){
            return Boolean.TRUE;
        }
        Teacher teacher = new Teacher();
        BeanUtil.copyProperties(teacherDTO,teacher);
        teacher.setCourseId(course.getId());
        teacher.setUpdateTime(LocalDateTime.now());
        teacher.setCreateTime(LocalDateTime.now());
        teacherService.saveOrUpdate(teacher);
        return res;
    }

    /**
     * 获取课程详情
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/getCourseById",method = RequestMethod.GET)
    public CourseDTO getCourseById(@RequestParam("courseId") Integer courseId){
        CourseDTO courseDTO = iCourseService.getCourseById(courseId);
        TeacherDTO teacherDTO = ConvertUtils.convert(teacherService.getByCourseId(courseId), TeacherDTO.class);
        courseDTO.setTeacherDTO(teacherDTO);
        return courseDTO;
    }

    @RequestMapping(value = "/getQueryCourses",method = RequestMethod.POST)
    public Page<CourseDTO> getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        Integer currentPage = courseQueryParam.getCurrentPage();
        Integer pageSize = courseQueryParam.getPageSize();
        Page<Course> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        IPage<Course> courseIPage = iCourseService.selectPage(page, wrapper);
        List<Course> records = courseIPage.getRecords();
        List<CourseDTO> CourseDTOs = ConvertUtils.convertList(records, CourseDTO.class);
        Page<CourseDTO> newCourseIPage = new Page<>();

        Page<CourseDTO> convert = ConvertUtils.convert(courseIPage, newCourseIPage);
        convert.setRecords(CourseDTOs);
        convert.setTotal(iCourseService.count(wrapper));
        return convert;
    }

    @RequestMapping(value = "/changeState",method = RequestMethod.POST)
    public boolean changeState(@RequestParam("courseId") Integer courseId, @RequestParam("status") Integer status){
        try{
            iCourseService.courseAutoOnline(courseId, status);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
